#include "uprogressbar.h"
#include "style/uprogressstyleoption.h"
#include "uapplication.h"
#include <QStylePainter>

class UProgressBarPrivate
{
public:
    UProgressBarPrivate() {}
    ~UProgressBarPrivate() {}

    int radius = 0;
    int borderWidth = 0;
    UPalette::ColorType bgRole = UPalette::SplitLineBackground;
    UPalette::ColorType borderRole = UPalette::ModuleBackground;
    UPalette::ColorType progressRole = UPalette::InformationBackground;

private:
    Q_DECLARE_PUBLIC(UProgressBar)
    Q_DISABLE_COPY(UProgressBarPrivate)
    UProgressBar*        q_ptr = nullptr;
};

UProgressBar::UProgressBar(QWidget* parent)
    : QProgressBar(parent), d_ptr(new UProgressBarPrivate())
{
    Q_D(UProgressBar);
    d->q_ptr = this;
}

UProgressBar::~UProgressBar()
{

}

void UProgressBar::setRadius(const int radius)
{
    Q_D(UProgressBar);
    if (d->radius == radius)
    {
        return;
    }
    d->radius = radius;
    update();
}

void UProgressBar::setBorderWidth(const int w)
{
    Q_D(UProgressBar);
    if (d->borderWidth == w)
    {
        return;
    }
    d->borderWidth = w;
    update();
}

void UProgressBar::setBorderRole(const UPalette::ColorType &role)
{
    Q_D(UProgressBar);
    if (d->borderRole == role)
    {
        return;
    }
    d->borderRole = role;
    update();
}

void UProgressBar::setBackgroundRole(const UPalette::ColorType &role)
{
    Q_D(UProgressBar);
    if (d->bgRole == role)
    {
        return;
    }
    d->bgRole = role;
    update();
}

void UProgressBar::setProgressRole(const UPalette::ColorType &role)
{
    Q_D(UProgressBar);
    if (d->progressRole == role)
    {
        return;
    }
    d->progressRole = role;
    update();
}

void UProgressBar::initStyleOption(UProgressStyleOption* option)
{
    Q_D(UProgressBar);
    QProgressBar::initStyleOption(option);
    option->radius = d->radius;
    option->borderWidth = d->borderWidth;
    option->borderRole = d->borderRole;
    option->bgRole = d->bgRole;
    option->progressRole = d->progressRole;
}

void UProgressBar::paintEvent(QPaintEvent*)
{
    QStylePainter paint(this);
    UProgressStyleOption opt;
    initStyleOption(&opt);
    paint.drawControl(QStyle::CE_ProgressBar, opt);
}
